﻿using System;
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace ET.Score.SharedLibrary
{
    public static class Util
    {
        /// <summary>
        /// 获取当前机器的全部网卡MAC地址
        /// </summary>
        /// <returns></returns>
        public static List<string> GetAllMac()
        {
            var macList = new List<string>();
            var netInterfaces = NetworkInterface.GetAllNetworkInterfaces();

            foreach (var item in netInterfaces)
            {
                if (IsAvaliable(item))
                {
                    macList.Add(GetMacAddressString(item.GetPhysicalAddress()));
                }
            }
            return macList;
        }

        private static string GetMacAddressString(PhysicalAddress macAddress)
        {
            var bytes = macAddress.GetAddressBytes();

            return BitConverter.ToString(bytes);
        }

        private static bool IsAvaliable(NetworkInterface item)
        {
            if (item.NetworkInterfaceType != NetworkInterfaceType.Ethernet
                && item.NetworkInterfaceType != NetworkInterfaceType.Wireless80211)
                return false;

            var ipProperties = item.GetIPProperties();

            return (from address in ipProperties.UnicastAddresses
                    where address.Address.AddressFamily == AddressFamily.InterNetwork
                    select address.Address.ToString())
                    .Any(ip => !ip.StartsWith("169.254."));
        }

        private static ConcurrentDictionary<Type, IEnumerable> emptyListCache =
            new ConcurrentDictionary<Type, IEnumerable>();

        /// <summary>
        /// 获取指定类型的泛型IList空对象，该对象为只读，只能用作Null-Object
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        public static IList<T> GetEmptyList<T>()
        {
            var result = emptyListCache.GetOrAdd(typeof(T), k => Enumerable.Empty<T>().ToList().AsReadOnly());
            return (IList<T>)result;
        }

        public static void CopyProperties(object src, object dest)
        {
            if (src == null || dest == null)
                return;
            Type srcType = src.GetType();
            Type destType = dest.GetType();

            var srcProps = srcType.GetProperties();
            var destProps = destType.GetProperties();

            foreach (var pSrc in srcProps)
            {
                var pDest = destProps.FirstOrDefault(item => item.Name == pSrc.Name);
                if (pDest == null)
                    continue;
                if (pDest.PropertyType != pSrc.PropertyType)
                    continue;

                pDest.SetValue(dest, pSrc.GetValue(src));
            }
        }

        public static int ParseIntegerOrDefault(IDictionary<string, string> settings, string key, int defaultValue)
        {
            string valueStr;

            if (settings.TryGetValue(key, out valueStr))
            {
                int interval;
                if (int.TryParse(valueStr, out interval))
                {
                    return interval;
                }
            }
            return defaultValue;
        }
    }
}
